package com.example.heathy_me;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.heathy_me.fragment.Card;
import com.example.heathy_me.fragment.Friend;
import com.example.heathy_me.fragment.Home;

import java.util.ArrayList;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    private AlarmManager alarmManager;
    private PendingIntent pi;
    private Fragment mCurrentFragment;
    private BottomNavigationBar mBottomNavigationBar;
    private ArrayList<Fragment> mFragmentList = new ArrayList<>();
    private int page =0;
    FragmentManager mFragmentManager;
    Calendar calendar;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //发送广播
        sendBroadcast(new Intent("finish"));

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent=getIntent();
        page =intent.getIntExtra("page",0);
        initView();
        MyApplication app = (MyApplication) getApplication();

/*
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.HOUR_OF_DAY, 20);//小时
        instance.set(Calendar.MINUTE, 25);//分钟
        instance.set(Calendar.SECOND, 0);//秒
        AlarmManager alarmService = (AlarmManager) this.getSystemService(ALARM_SERVICE);
        Intent alarmIntent = new Intent(this, ScreenControlAlarmReceiver.class).setAction("intent_alarm_log");
        PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);//通过广播接收
        alarmService.set(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis() , broadcast);
   */

        //分割线
        if(!app.isLogin()){
            Intent intent1=new Intent(MainActivity.this,Login.class);
            startActivity(intent1);
        }



    }

    private void initView(){
        mFragmentManager=getSupportFragmentManager();
        initFragment();
        showFragment(page);
        mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        mBottomNavigationBar.setBarBackgroundColor(android.R.color.white);
        mBottomNavigationBar
                .setActiveColor(R.color.colorBottomBack) //设置选中的颜色
                .setInActiveColor(R.color.colorActive); //未选中
        mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.home, "首页"))
                .addItem(new BottomNavigationItem(R.drawable.friend, "好友"))
                .addItem(new BottomNavigationItem(R.drawable.card, "医疗急救卡"))
                .setFirstSelectedPosition(page)
                .initialise();
        mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){

            @Override
            public void onTabSelected(int position) {
                Log.d("调试日志",""+position);
                showFragment(position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }


    private void initFragment(){
        mFragmentList.add(new Home());
        mFragmentList.add(new Friend());
        mFragmentList.add(new Card());

    }
    private void showFragment(int page) {
        FragmentTransaction mFragmentTransaction = mFragmentManager
                .beginTransaction();
        if (mCurrentFragment != null) {
            mFragmentTransaction.hide(mCurrentFragment);
        }
        mCurrentFragment = mFragmentList.get(page);
        if (mCurrentFragment.isAdded())
        {
            mFragmentTransaction.show(mCurrentFragment);
        }else {
            mFragmentTransaction.add(R.id.fragment, mCurrentFragment);
        }
        mFragmentTransaction.commitAllowingStateLoss();
    }
    public void setAlarmOnetoNotification(int time, boolean issetalarm) {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent();
        intent.setAction("com.example.yy.game_notification");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0x101, intent, 0);
        if (issetalarm) {
            long triggerAtTime = SystemClock.elapsedRealtime() + time; //获取手机开机到现在的时间 加上你要延时的时间
            alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pendingIntent);//未设提醒，设置提醒
        } else {
            alarmManager.cancel(pendingIntent);//已设提醒，取消之前提醒，重新设置
        }
    }
}
